package com.doit.day02

import scala.collection.mutable.ListBuffer

object _02_算子 {
  def main(args: Array[String]): Unit = {
    val list: List[Int] = List(1, 2, 3, 4, 5)

    //这个函数是将每一个元素都乘以10
    /*val ma = (a:Int)=>{a*10}

    val ints: List[Int] = list.map(ma)*/

    val lines: List[String] = List("hello,mingge,hello,feige","hello,chujiejie,hello,xiongge")
    //我想得到一个个的单词
//    List(List("hello","mingge","hello","feige"),List("hello","chujiejie","hello","xiongge"));

    val list1 :List[List[String]]= lines.map((line: String) => {
      line.split(",").toList
    })

    val flatten: List[String] = list1.flatten

    println(flatten)

    //flatMap
    val flattened: List[String] = lines.flatMap((line: String) => {
      line.split(",").toList
    })


    val words: List[String] = List("hello", "hello", "hi", "hive", "hive", "hi", "hive")

    /**
     * hello ==》 List("hello", "hello")
     * hi    ==》 List("hi","hi")
     * hive  ==》 List("hive", "hive","hive")
     */
    val map: Map[String, List[String]] = words.groupBy((word: String) => word)

    val wordAndCnt: Map[String, Int] = map.map((keyvalue) => {
      val key: String = keyvalue._1 //这个key代表的就是一个个的单词
      val value: List[String] = keyvalue._2
      val size: Int = value.size
      (key, size)
    })

    println(wordAndCnt)














  }
}
